<?php

class advertise extends cms_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('advertise_model', 'adv');
        $this->load->model('invoicies_model', 'invoice');
    }

    public function index() {
        $this->pass['result'] = $this->adv->get(array('user_add' => $this->user_id));
        $this->themes->render('advertise/index', $this->pass);
    }

    private function upload_file($input) {

        $config['upload_path'] = ADS_PATH . '/temp/';
        $config['file_name'] = md5(time() . $this->session->userdata('user_NAME') . $input);
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1024';
        $config['min_width'] = '210';
        $config['min_height'] = '260';
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($input)) {
            return $this->upload->display_errors();
        } else {
            $filedata = $this->upload->data();
            $this->session->set_userdata($input, $filedata['file_name']);
            return true;
        }
    }

    function validlink($str) {
        $pattern = '|^(?:https?://)?[a-z0-9%-]+(?:\.[a-z0-9%-]+)*(?::[0-9]+)?(/[a-z0-9%-]+)?$|i';
        if (preg_match($pattern, $str)) {
            return TRUE;
        } else {
            $this->form_validation->set_message('validlink', lang('message_valid_url'));
            return FALSE;
        }
    }

    public function add() {
        $this->pass['click_price'] = $click_price = get_option('adv_click_price');
        $this->pass['view_price'] = $view_price = get_option('adv_view_price');
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->form_validation->set_rules('url', lang('label_link'), 'required|trim|callback_validlink');
            $this->form_validation->set_rules('showtype', lang('label_adv_show_type'), 'required|trim');
            if ($this->form_validation->run()) {
                $type = $this->input->post('showtype');
                if (in_array($type, array('click', 'view'))) {
                    if ($type == 'click') {
                        if ($this->input->post('clicksnum') && $this->input->post('clickscost')) {
                            $save['max_click'] = (int) ($this->input->post('clickscost') / $click_price);
                            $save['max_view'] = '0';
                            $cost = $this->input->post('clickscost');
                        } else {
                            $ms = str_replace('%s', lang('label_cost'), lang('message_wrong_data'));
                            $this->themes->set_message(array('url' => current_url(), 'type' => 'validation_error', 'message' => $ms));
                        }
                    } elseif ($type == 'view') {
                        if ($this->input->post('viewsnum') && $this->input->post('viewscost')) {
                            $save['max_view'] = (int) ($this->input->post('viewscost') / $view_price);
                            $save['max_click'] = '0';
                            $cost = $this->input->post('viewscost');
                        } else {
                            $ms = str_replace('%s', lang('label_cost'), lang('message_wrong_data'));
                            $this->themes->set_message(array('url' => current_url(), 'type' => 'validation_error', 'message' => $ms));
                        }
                    } else {
                        $ms = str_replace('%s', lang('label_adv_show_type'), lang('message_wrong_data'));
                        $this->themes->set_message(array('url' => current_url(), 'type' => 'validation_error', 'message' => $ms));
                    }
                } else {
                    $ms = str_replace('%s', lang('label_adv_show_type'), lang('message_wrong_data'));
                    $this->themes->set_message(array('url' => current_url(), 'type' => 'validation_error', 'message' => $ms));
                }
                $upload = $this->upload_file("image");
                if ($this->session->userdata("image")) {
                    if (@copy(ADS_PATH . "/temp/" . $this->session->userdata("image"), ADS_PATH . "/" . $this->session->userdata("image"))) {
                        @unlink(ADS_PATH . "/temp/" . $this->session->userdata("image"));
                        $save['image'] = $this->session->userdata("image");
                    }
                } else {
                    $this->themes->set_message(array('url' => current_url(), 'type' => 'validation_error', 'message' => $upload));
                }
                $save['active'] = 'NO';
                $save['url'] = $this->input->post('url');
                $save['user_add'] = $this->user_id;
                $save['inv_num'] = $inv = $this->unique_id();
                $save['type'] = 'ads';
                $save['view'] = '0';
                $save['click'] = '0';

                if ($this->adv->save($save)) {
                    $invsave['inv_num'] = $inv;
                    $invsave['title'] = 'label_buy_adv';
                    $invsave['user_id'] = $this->user_id;
                    $invsave['total'] = $cost / 100;
                    $invsave['must_pay'] = $cost / 100;
                    $invsave['for'] = 'advertise';
                    $this->invoice->save($invsave);
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_add_success'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        }

        $this->themes->render('advertise/add', $this->pass);
    }

    private function unique_id() {
        $string = strrev(md5(time() + $this->session->userdata('session_id')));
        $unique = substr($string, 0, 9);
        $temp = $this->invoice->get(array('inv_num' => $unique));
        if (count($temp) > 0) {
            $this->unique_id();
        } else {
            return $unique;
        }
        return false;
    }

    function clickad($id = false) {
        if (is_numeric($id)) {
            $data = $this->adv->get(array('id' => $id), false, 'row');
            if ($data) {
                inc_adv_click($id);
            }
        }
        return false;
    }

}

?>
